#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>

#include <cassert>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace dsh_model
{
    using namespace dsh_util;
    using namespace dsh_log;

    struct Question
    {
        std::string number; // 题目编号
        std::string title;  // 题目标题
        std::string star;   // 难度
        int cpu_limit;      // cpu时间限制
        int mem_limit;      // 空间限制
        std::string desc;   // 题目的描述
        std::string header; // 题目预设给用户在线编译器的代码
        std::string tail;   // 题目的测试用例，要和header拼接
    };

    const std::string questions_list = "./questions/questions.list";
    const std::string questions_path = "./questions/";

    class Model
    {
    public:
        Model()
        {
            assert(LoadQuestionList(questions_list));
        }
        ~Model()
        {}

        bool LoadQuestionList(const std::string& question_list)
        {
            // 加载配置文件
            // 打开questions_list文件，按行读取
            std::ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "题库加载失败，题库文件出错！" << std::endl;
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            {
                std::vector<std::string> tokens;
                StringUtil::splitString(line, &tokens, " ");
                // 1 反转字符串 简单 1 30000
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，文件格式出错" << std::endl;
                    continue;
                }

                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                std::string path = questions_path;
                path += q.number + "/";

                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);

                questions.insert(make_pair(q.number, q));
            }
            LOG(INFO) << "加载题库成功" << std::endl;
            in.close(); 
            return true;
        }

        bool GetAllQuestions(std::vector<Question>* out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题目失败" << std::endl;
                return false;
            }
            for (auto& q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestion(const std::string& number, Question* q)
        {
            const auto& iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败，题目编号：" << number << std::endl;
                return false;
            }
            (*q) = iter->second;
            return true;
        }

    private:
        std::unordered_map<std::string, Question> questions; // 题目 : 题目细节
    };
}